package com.sz.web.account.control;


import com.sz.web.common.pojo.ReturnPojo;
import com.sz.web.account.entity.AccountUser;
import com.sz.web.account.service.AccountUserService;
import com.sz.web.common.service.BaseService;
import com.sz.web.util.WebUserHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

@Controller
@RequestMapping(value = "/user")
public class AccountUserControl {

    @Resource
    private AccountUserService userService;
    @Resource
    private BaseService<AccountUser> accountUserBaseService;

    /**
     * 登录
     * @param accountUser
     * @return
     */
    @PostMapping(value = "/login")
    public String login(AccountUser accountUser, Model model){
        ReturnPojo result = userService.login(accountUser);
        if(result.isSuccess()){
            WebUserHelper.setAccountInfo(result.getDatas());
            model.addAttribute("accountInfo",result.getDatas());
            return "index";
        }
        return "redirect:/web";
    }

    /**
     * 注册
     * @param accountUser
     * @return
     */
    @PostMapping(value = "/register")
    public ReturnPojo register(AccountUser accountUser){
        return userService.register(accountUser);
    }


    @PostMapping(value = "/insert")
    public ReturnPojo insert(AccountUser accountUser){
        return accountUserBaseService.insert(accountUser);
    }

    @PostMapping(value = "/delete")
    public ReturnPojo delete(AccountUser accountUser){
        return accountUserBaseService.delete(accountUser);
    }
}
